package middlewares

import (
	//"fmt"
	"gintest/until"
	"github.com/gin-gonic/gin"
	"net/http"
)


func Inits() gin.HandlerFunc {
	// Do some initialization logic here

	// Foo()

	return func(c *gin.Context) {




		method := c.Request.Method
		/*c.Writer.Header().Set("Access-Control-Allow-Origin","*")
		c.Writer.Header().Set("Access-Control-Allow-Headers","Content-Type,AccessToken,X-CSRF-Token, Authorization, Token")
		c.Writer.Header().Set("Access-Control-Allow-Methods","POST, GET, OPTIONS")
		c.Writer.Header().Set("Access-Control-Expose-Headers","Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Content-Type")
		c.Writer.Header().Set("Access-Control-Allow-Credentials","true")*/
		c.Header("Cache-Control","no-cache")
		c.Header("Access-Control-Allow-Origin", "*")
		c.Header("Access-Control-Allow-Headers", "Content-Type,AccessToken,X-CSRF-Token, Authorization, Token")
		c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS")
		c.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Content-Type")
		c.Header("Access-Control-Allow-Credentials", "true")
		//放行所有OPTIONS方法
		if method == "OPTIONS" {
			c.AbortWithStatus(http.StatusNoContent)
		}
		c.Next()
	}

}
func Auth() gin.HandlerFunc {
	// Do some initialization logic here

	// Foo()

	return func(c *gin.Context) {
		ses:=c.GetHeader("Token")
		token,err:= until.ReadToken(ses,"ses_")
		if  err!=nil {
			c.Abort()
			until.Fail(c,1,"未登陆")
		}else{
			c.Set("token", token)
		}
		c.Next()

	}

}
func AuthStore() gin.HandlerFunc {
	// Do some initialization logic here

	// Foo()

	return func(c *gin.Context) {
		ses:=c.GetHeader("Token")
		token,err:= until.ReadToken(ses,"store_")
		if  err!=nil {
			c.Abort()
			until.Fail(c,1,"未登陆")
		}else{
			c.Set("token", token)
		}
		c.Next()

	}

}
func AuthApi() gin.HandlerFunc {
	// Do some initialization logic here

	// Foo()

	return func(c *gin.Context) {
		ses:=c.GetHeader("Token")
		token,err:= until.ReadToken(ses,"api_")
		if  err!=nil {
			c.Abort()
			until.Fail(c,1,"未登陆")
		}else{
			c.Set("token", token)
		}
		c.Next()

	}

}